package com.partner.frame.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import sun.nio.cs.ext.MacThai;

import com.partner.bean.member.Member;
import com.partner.bean.order.Order;
import com.partner.bean.order.Ordermi;
import com.partner.frame.support.util.DateConvertUtil;
import com.partner.frame.util.MemberUtil;
import com.partner.service.member.MemberService;
import com.partner.service.order.OrderService;
import com.partner.service.order.OrdermiService;
import com.partner.service.sysbase.SystemSettingService;

/** 
 * 数据处理积分冻结15天
 * @author dwr
 * @date 2018-09-04 
 */  
@Component 
public class WorkTask {
	
	@Autowired
	private SystemSettingService systemSettingService ; 
	@Autowired
	private OrderService orderService;
	@Autowired
	private MemberService memberService;
	boolean isxh = true ; 
	//private final static Logger log = LoggerFactory.getLogger(WorkTask.class);
	
	/**  
     *  15天后解冻积分，并且把积分累加到用户可提现积分字段中
     * @throws ParseException 
     */    
	@Scheduled(cron = "0 */5 * * * ?")   
	public void downLoadBizUserMacData() throws Exception {
    
    	//System.out.println("定时器数据处理.................");
		//处理积分冻结业务
		if(isxh){
				Order ord = new Order();
		    	ord.setDisabled(0);
		    	List<Order> ordermiList = orderService.selectAll(ord);//查询出已激活订单数据
		    	for(int i =0 ; i < ordermiList.size() ; i++){
		    		isxh = false ; 
		    		Order om = ordermiList.get(i);
		    		if(om.getCreatetime()!=null){
		    			int daynum = DateConvertUtil.differentDaysByMillisecond(om.getCreatetime(),new Date());//计算相隔天数
		    			long minnum = DateConvertUtil.getMinPoor(new Date(), om.getCreatetime());//计算相隔分钟
		    			if(minnum >= 5 ){
		    				Member umember = new Member();
		    				umember.setId(om.getMemberid());
		    				//取出用户原用户信息
		    				Member oldmember = memberService.selectByPrimaryId(om.getMemberid());
		    				umember.setWithdrawintegral(oldmember.getWithdrawintegral()+om.getSettleaccounts());
		    				memberService.updateByPrimaryKey(umember);//更新可提现积分
		    				//更新已激活订单，更新冻结积分状态为解冻
		    				Order upom = new Order();
		    				upom.setId(om.getId());
		    				upom.setDisabled(1);//1：解冻
		    				upom.setActivationtime(new Date());
		    				orderService.updateByPrimaryKey(upom);
		    			}
		    		}
		    		if(i==ordermiList.size()-1){
			    		isxh = true ; 
			    	}
		    	}
		 }
	}
}
